import 'reflect-metadata'

type MyPropDecorator = (targetClassPrototype: any, propertyKey: string | symbol) => void

export function Autowired(dependencyid?: string): MyPropDecorator {
    return (targetClassPrototype, propertyKey) => {
        console.log("执行属性装饰器：执行的类是==>", targetClassPrototype.constructor, " 属性名是==>", propertyKey);

        // PropServiceClass = UserServiceInter 为接口类
        let PropServiceClass = Reflect.getMetadata("design:type", targetClassPrototype, propertyKey);
        // 如使用了Singleton装饰器，则：单例--ServiceImplInstanceOrClass 和 metaSingleton 都有值；多例--ServiceImplInstanceOrClass有值.
        // 如果没有使用Singleton装饰器，则下面2个均不会有值
        let ServiceImplInstanceOrClass = Reflect.getMetadata("ServiceImplInstanceOrClass", targetClassPrototype, propertyKey);
        let metaSingleton = Reflect.getMetadata("singleton", targetClassPrototype, propertyKey);

        let ServiceImplInstance: any;
        if (metaSingleton) {
            ServiceImplInstance = ServiceImplInstanceOrClass
        } else {
            //如果没有使用Singleton装饰器，则ServiceImplInstanceOrClass没有值
            if (!ServiceImplInstanceOrClass) {
                ServiceImplInstanceOrClass = PropServiceClass.getImplClass();
            }
            ServiceImplInstance = new ServiceImplInstanceOrClass();
        }
        //保存对象
        Reflect.defineProperty(targetClassPrototype, propertyKey, {value: ServiceImplInstance});
    }
}
